<?php
defined('IN_CMS') or die('Direct access to Script restricted.');

class Session extends Common
{

	/**
	 * Конструктор класса
	 */
	public function __construct()
	{
		
	}
	
	/**
	 * Деструктор класса
	 */
	public function __destruct()
	{

	}
	
	/*
	 * 
	 */
	public function open($save_path, $session_name) 
	{
		global $sess_save_path, $sess_session_name;

		$sess_save_path = $save_path;
		$sess_session_name = $session_name;

		return(true);
	}
	
	/*
	 * 
	 */
	public function close() 
	{
		return(true);
	}
	
	/*
	 * 
	 */
	public function read($id) 
	{
		$id = mysql_real_escape_string($id);
		
		$sql = "SELECT 
					session_data 
				FROM 
					" . TABLE_PREFIX . "sessions 
				WHERE 
					id = '$id' 
				LIMIT 1";
		
		//self::$obj_error->showMessage($sql, 3);
		
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		if ( self::$obj_db->sql_numrows($result) > 0 )
		{
			$row = self::$obj_db->sql_fetchassoc($result);
			return $row['session_data'];
		}
		
		return '';
	}
	
	/*
	 * 
	 */
	public function write($id, $data) 
	{
		$access = time();
		$id = mysql_real_escape_string($id);
		$access = mysql_real_escape_string($access);
		$data = mysql_real_escape_string($data);
		
		$sql = "REPLACE INTO 
					" . TABLE_PREFIX . "sessions 
				VALUES 
				(
					'$id',
					'$access',
					'$data'
				)";
		
		//self::$obj_error->showMessage($sql, 3);
		
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		return true;
	}
	
	/*
	 * 
	 */
	public function destroy($id) 
	{
		$id = mysql_real_escape_string($id);
		
		$sql = "DELETE FROM 
					" . TABLE_PREFIX . "sessions 
				WHERE 
					id = '$id'";
		
		//self::$obj_error->showMessage($sql, 3);
		
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		return true;
	}
	
	/*
	 * 
	 */
	public function gc($max) 
	{
		$old = time() - $max;
		$old = mysql_real_escape_string($old);
		
		$sql = "DELETE FROM 
					" . TABLE_PREFIX . "sessions 
				WHERE 
					expires < '$old'";
		
		//self::$obj_error->showMessage($sql, 3);
		
		$result = self::$obj_db->sql_query($sql) or self::$obj_error->showMessageSQL(__FUNCTION__, basename(__FILE__), __LINE__);
		return true;
	}
}
?>